package com.gettec.lims.client.grid;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BasePagingLoader;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelReader;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MenuEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.GroupingStore;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridGroupRenderer;
import com.extjs.gxt.ui.client.widget.grid.GroupColumnData;
import com.extjs.gxt.ui.client.widget.grid.GroupingView;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FlowData;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.extjs.gxt.ui.client.widget.menu.Menu;
import com.extjs.gxt.ui.client.widget.menu.MenuBar;
import com.extjs.gxt.ui.client.widget.menu.MenuBarItem;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;
import com.gettec.lims.client.NlimsConstants;
import com.gettec.lims.client.forms.SampleInfoForm;
import com.gettec.lims.client.menu.MenuItemEx;
import com.gettec.lims.client.resources.Resources;
import com.gettec.lims.client.services.NnimsServiceAsync;
import com.gettec.lims.shared.model.SampleInfo;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class SampleInfoGridView extends Window{
	 private NnimsServiceAsync service;
	  Grid<BeanModel> grid;
	  GroupingStore<BeanModel> store ;
	   ContentPanel panel = new ContentPanel();   
public SampleInfoGridView()
{
	setWidth(900);//必须在这设置才有效
   
   Menu menu = new Menu();   
   
   MenuItemEx item1 = new MenuItemEx("样品信息登记","新增一般样品信息"); 
   item1.addSelectionListener(new SelectionListener<MenuEvent>()
     		 {

 				@Override
 				public void componentSelected(MenuEvent ce) {
 					// TODO Auto-generated method stub
 					SampleInfo rs=new SampleInfo();
 					SampleInfoForm rf=	new SampleInfoForm(service,store, rs);
 					rf.show();
 				}
     	 
     		 });
   menu.add(item1);   
 
   MenuItemEx item2 = new MenuItemEx("样品信息登记","修改记录数据");   
   menu.add(item2); 
  
   MenuItemEx item3 = new MenuItemEx("样品信息登记","删除记录数据");   
   menu.add(item3); 
  
   MenuBar bar = new MenuBar();   
   bar.setBorders(true);   
   bar.setStyleAttribute("borderTop", "none");   
   bar.add(new MenuBarItem("文件", menu));   
 
   Menu sub2 = new Menu(); 
   MenuItemEx item4=new MenuItemEx("样品信息登记","检测配置任务");
   sub2.add(item4);   
   
 
   MenuBarItem edit = new MenuBarItem("检测", sub2);   
   bar.add(edit);   

   panel.setHeading("样品信息管理");   
   panel.setTopComponent(bar);   
   add(panel, new FlowData(10));   
}
	@Override
	protected void onRender(Element parent, int index) {
		// TODO Auto-generated method stub
		super.onRender(parent, index);
		  service = (NnimsServiceAsync) Registry.get(NlimsConstants.SERVICE);
	    RpcProxy<PagingLoadResult<SampleInfo>> proxy = new RpcProxy<PagingLoadResult<SampleInfo>>() {   
	      @Override  
	      public void load(Object loadConfig,   
	          AsyncCallback<PagingLoadResult<SampleInfo>> callback) {   
	        
	        service.getSampleInfos((PagingLoadConfig) loadConfig, callback);   
	      }   
	    };   
	  
	    // loader   
	    final BasePagingLoader<PagingLoadResult<ModelData>> loader = new BasePagingLoader<PagingLoadResult<ModelData>>(   
	        proxy, new BeanModelReader());   
	    loader.setRemoteSort(true);   
	  
	   // store = new ListStore<BeanModel>(loader);   
	   // GroupingStore<BeanModel>
	    store = new GroupingStore<BeanModel>(loader);   
	    store.groupBy("order_serial_no");   

	    final PagingToolBar toolBar = new PagingToolBar(50);   
	    toolBar.bind(loader);   
	    FormPanel fpanel = new FormPanel();
	  
	    grid = createGrid(store);   
	    grid.addListener(Events.Attach, new Listener<GridEvent<BeanModel>>() {   
	      public void handleEvent(GridEvent<BeanModel> be) {   
	        loader.load(0, 50);   
	      }   
	    });   
	    createForm(grid,store,fpanel);
	  
	 
	    panel.setFrame(true);   
	    panel.setCollapsible(true);   
	    panel.setAnimCollapse(false);   
	    panel.setButtonAlign(HorizontalAlignment.CENTER);   
	    panel.setIcon(Resources.ICONS.table());   
	  
	    panel.setHeaderVisible(false);
	     
	    //panel.add(grid);   
	    panel.setSize(900, 350);   
	    panel.setBottomComponent(toolBar);   
	    panel.setLayout(new RowLayout(Orientation.HORIZONTAL)); 
	   // panel.setFrame(true);   
	  
	    panel.add(grid, new RowData(.6, 1));   
	    panel.add(fpanel, new RowData(.4, 1));   
	    layout();
	   // add(panel);   
	  
	  } 
	private FormPanel createForm( Grid<BeanModel> grid,ListStore<BeanModel> store,FormPanel panel ) 
	{
		panel.setHeaderVisible(false);   
	    panel.setScrollMode(getScrollMode().AUTOY);
	     final TextField<String> name = new TextField<String>();  
		 final TextField<String> resource_id = new TextField<String>();  
		 final TextField<String> data_coverage_id = new TextField<String>();
		 final TextField<String> abstract_sample_id = new TextField<String>();   
		 final TextField<String> product_info_c_id = new TextField<String>();   
		 final TextField<String> product_category_id = new TextField<String>();
		 final TextField<String> product_type_id = new TextField<String>();  
		 final TextField<String> backup_storage_place_id = new TextField<String>();  
		 final TextField<String> sample_amount_value_unit_id = new TextField<String>();
		 final TextField<String> provider_info_id = new TextField<String>();  
		 final TextField<String> test_type_id = new TextField<String>();   
		 final TextField<String> volume_for_test = new TextField<String>();  
		 final TextField<String> volume_for_backup = new TextField<String>(); 
		 final TextField<String> order_serial_no = new TextField<String>(); 
		 final TextField<String> claimed_brand = new TextField<String>();  
		 final TextField<String> level = new TextField<String>();
		 final TextField<String> type = new TextField<String>();  
		 final TextField<String> format = new TextField<String>();  
		 final TextField<String> regularity = new TextField<String>();
		 final TextField<String> production_date = new TextField<String>();
		 final TextField<String> batch_serial_no = new TextField<String>();
		 final TextField<String> serial = new TextField<String>();
		 final TextField<String> status = new TextField<String>();
		 final TextField<String> price = new TextField<String>();
		 final TextField<String> inventory_volume = new TextField<String>();
		 final TextField<String> purchase_volume = new TextField<String>();
		 final TextField<String> air_temperature = new TextField<String>();
		 final TextField<String> humidity = new TextField<String>();
		 final TextField<String> notes = new TextField<String>();
		String[] zh_captions={"样品名称","图片","数据范围","抽象样品ID","小类","牌号","型号","备份样品封存地","样品数量单位","生产企业",
				"抽样类型","检测用样品数量","备份用样品数量","委托登记号","标称商标","等级","型号","规格","标称标准","生产日期" , "批次",
				"样品编号","样品状态","销售单价","库存量","进货量","气温","湿度","备注"};
		Field[]fields={name,resource_id,data_coverage_id,abstract_sample_id,product_info_c_id,product_category_id,product_type_id,backup_storage_place_id,sample_amount_value_unit_id,provider_info_id,test_type_id,volume_for_test,volume_for_backup,order_serial_no,claimed_brand,level,type,format,regularity,production_date,batch_serial_no,serial,status,price,inventory_volume,purchase_volume,air_temperature,humidity,notes};
		String[]captions={												     
				"name","resource_id","data_coverage_id","abstract_sample_id","product_info_c_id",
				"product_category_id","product_type_id","backup_storage_place_id","sample_amount_value_unit_id",
				"provider_info_id","test_type_id","volume_for_test","volume_for_backup","order_serial_no",
				"claimed_brand","level","type","format","regularity","production_date","batch_serial_no",
				"serial","status","price","inventory_volume","purchase_volume","air_temperature","humidity",
				"notes"};
		 FormData formData = new FormData("100%");     
		    panel.setFrame(true);   
		   for( int i=0;i<fields.length;i++)
			{
		    	
				fields[i].setName(captions[i]);
				fields[i].setFieldLabel(zh_captions[i]);
		    	
				panel.add(fields[i], formData);   
					
			}
			 final FormBinding formBindings = new FormBinding(panel,true);//绑定必须在panel加入控件之后
			   formBindings.setStore(store);  
			 grid.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);   
			    grid.getSelectionModel().addListener(Events.SelectionChange,   
			        new Listener<SelectionChangedEvent<BeanModel>>() {   
			          public void handleEvent(SelectionChangedEvent<BeanModel> be)
			          {   
			            if (be.getSelection().size() > 0) {
			            	ModelData md=(ModelData) be.getSelection().get(0);
			              formBindings.bind(md);   
			            } else {   
			              formBindings.unbind();   
			            }   
			          }   
			        });  
			    return panel;
	}
	private Grid<BeanModel> createGrid(ListStore<BeanModel> store) {   
		 
		
	    store.setMonitorChanges(true); 
	    List<ColumnConfig> columns = new ArrayList<ColumnConfig>();  
		  
	    columns.add(new ColumnConfig("order_serial_no", "委托登记号", 150));
	    columns.add(new ColumnConfig("name", "样品名", 150));   
	    columns.add(new ColumnConfig("product_info_c_id", "商品小类", 100));   
	     columns.add(new ColumnConfig("product_category_id", "商品编号", 200));   
	    columns.add(new ColumnConfig("serial", "样品编号", 100));   
	    columns.add(new ColumnConfig("volume_for_test", "样品数量", 200)); 
	    final ColumnModel cm = new ColumnModel(columns); 
	    GroupingView view = new GroupingView();   
	    view.setShowGroupedColumn(false);   
	    view.setForceFit(true);   
	    view.setGroupRenderer(new GridGroupRenderer() {   
	      public String render(GroupColumnData data) {   
	        String f = cm.getColumnById(data.field).getHeader();   
	        String l = data.models.size() == 1 ? "Item" : "Items";   
	        return f + ": " + data.group + " (" + data.models.size() + " " + l + ")";   
	      }   
	    });   

	    Grid<BeanModel> grid = new Grid<BeanModel>(store, cm);  
	    grid.setView(view);   

	    grid.getView().setEmptyText("  ");   
	    grid.setLayoutData(new FitLayout());
	    grid.setLoadMask(true);   
	    grid.setBorders(true);   
	    grid.setAutoExpandColumn("name");  

	  return grid;
	   
	}
	
}
